package com.hk.core.autoconfigure.data.database;

import com.hk.core.jdbc.tenants.UserTenantAware;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Map;

/**
 * @author Kevin
 * @date 2023-10-25 22:09
 */
class DynamicDataSource extends AbstractRoutingDataSource {

    private final UserTenantAware userTenantAware;

    DynamicDataSource(UserTenantAware tenantInfoHolder,
                      DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
        this.userTenantAware = tenantInfoHolder;
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return userTenantAware.getCurrentTenantId();
    }
}
